package com.nanhai.buddhist.college.module.study.service.attendanceleavereason;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.query.QueryWrapperX;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendancemanagement.AttendanceManagementDO;
import com.nanhai.buddhist.college.module.study.dal.mysql.attendancemanagement.AttendanceManagementMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.study.controller.admin.attendanceleavereason.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendanceleavereason.AttendanceLeaveReasonDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.attendanceleavereason.AttendanceLeaveReasonConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.attendanceleavereason.AttendanceLeaveReasonMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 请假事由 Service 实现类
 *
 * @author Akai
 */
@Service
@Validated
public class AttendanceLeaveReasonServiceImpl implements AttendanceLeaveReasonService {

    @Resource
    private AttendanceLeaveReasonMapper attendanceLeaveReasonMapper;

    @Resource
    private AttendanceManagementMapper attendanceManagementMapper;

    @Override
    public String createAttendanceLeaveReason(AttendanceLeaveReasonCreateReqVO createReqVO) {
        // 插入
        AttendanceLeaveReasonDO attendanceLeaveReason = AttendanceLeaveReasonConvert.INSTANCE.convert(createReqVO);
        attendanceLeaveReasonMapper.insert(attendanceLeaveReason);
        // 返回
        return attendanceLeaveReason.getId();
    }

    @Override
    public void updateAttendanceLeaveReason(AttendanceLeaveReasonUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateAttendanceLeaveReasonExists(updateReqVO.getId());
        // 更新
        AttendanceLeaveReasonDO updateObj = AttendanceLeaveReasonConvert.INSTANCE.convert(updateReqVO);
        attendanceLeaveReasonMapper.updateById(updateObj);
    }

    @Override
    public Boolean deleteAttendanceLeaveReason(String id) {
        // 校验存在
        this.validateAttendanceLeaveReasonExists(id);
        //校验是否有业务关联
        if(this.validateAttendanceLeaveReasonAssociated(id)){
            return false;
        }

        // 删除
        attendanceLeaveReasonMapper.deleteById(id);
        return true;
    }


    /**
     * 查询请假类型是否关联业务表数据
     * @param id 请假类型id
     * @return 有关联返回true，否则返回false
     */
    private boolean validateAttendanceLeaveReasonAssociated(String id) {
        return attendanceManagementMapper.exists(new LambdaQueryWrapperX<AttendanceManagementDO>().eqIfPresent(AttendanceManagementDO::getReasonId, id));
    }

    private void validateAttendanceLeaveReasonExists(String id) {
        if (attendanceLeaveReasonMapper.selectById(id) == null) {
            throw exception(ATTENDANCE_LEAVE_REASON_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceLeaveReasonDO getAttendanceLeaveReason(String id) {
        return attendanceLeaveReasonMapper.selectById(id);
    }

    @Override
    public List<AttendanceLeaveReasonDO> getAttendanceLeaveReasonList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : attendanceLeaveReasonMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AttendanceLeaveReasonDO> getAttendanceLeaveReasonPage(AttendanceLeaveReasonPageReqVO pageReqVO) {
        return attendanceLeaveReasonMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AttendanceLeaveReasonDO> getAttendanceLeaveReasonList(AttendanceLeaveReasonExportReqVO exportReqVO) {
        return attendanceLeaveReasonMapper.selectList(exportReqVO);
    }

    @Override
    public List<AttendanceLeaveReasonDO> getAttendanceLeaveReasonAllList() {
        return  attendanceLeaveReasonMapper.selectList(new LambdaQueryWrapperX<>());
    }

}
